using UnityEngine;
using TMPro;
using System.Collections;
using System.Collections.Generic;

public class DamagePopupController : MonoBehaviour
{
    public static DamagePopupController Instance { get; private set; }

    [Header("飘字预制体")]
    public GameObject damagePopupPrefab;

    [Header("飘字设置")]
    public float popupDuration = 1f;
    public float floatDistance = 1f;

    private Dictionary<ITarget, MonoBehaviour> targetViewMap = new Dictionary<ITarget, MonoBehaviour>();

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            // 可选：DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    private void Start()
    {
        // 订阅伤害事件
        EffectSystem.OnDamageApplied += OnDamageApplied;

        // 自动注册现有的 View
        RegisterExistingViews();
    }

    private void RegisterExistingViews()
    {
        // 注册所有 EnemyView
        var enemyViews = FindObjectsOfType<EnemyView>();
        foreach (var enemyView in enemyViews)
        {
            if (enemyView.enemyData != null)
            {
                targetViewMap[enemyView.enemyData] = enemyView;
            }
        }

        // 注册 PlayerView
        var playerView = FindObjectOfType<PlayerView>();
        if (playerView != null && playerView.playerData != null)
        {
            targetViewMap[playerView.playerData] = playerView;
        }
    }

    public void RegisterTargetView(ITarget target, MonoBehaviour view)
    {
        if (!targetViewMap.ContainsKey(target))
        {
            targetViewMap[target] = view;
        }
    }

    private void OnDamageApplied(ITarget target, int damageAmount)
    {
        if (damageAmount <= 0) return;

        // 获取对应的 View 组件
        if (targetViewMap.TryGetValue(target, out MonoBehaviour view))
        {
            Vector3 worldPosition = view.transform.position + Vector3.up * 20f;
            Color popupColor = (target is PlayerData) ? Color.red : Color.yellow;

            CreatePopup(worldPosition, damageAmount, popupColor);
        }
        else
        {
            Debug.LogWarning($"未找到目标 {target} 对应的 View 组件");
        }
    }

    public void CreatePopup(Vector3 position, int damage, Color color)
    {
        if (damagePopupPrefab == null)
        {
            Debug.LogWarning("伤害飘字预制体未设置！");
            return;
        }

        GameObject popup = Instantiate(damagePopupPrefab, position, Quaternion.identity);
        TextMeshPro textMesh = popup.GetComponentInChildren<TextMeshPro>();

        if (textMesh != null)
        {
            textMesh.text = "-"+damage.ToString();
            textMesh.color = color;
        }

        StartCoroutine(AnimatePopup(popup));
    }

    private IEnumerator AnimatePopup(GameObject popup)
    {
        float elapsedTime = 0f;
        Vector3 startPosition = popup.transform.position;
        Vector3 endPosition = startPosition + Vector3.up * floatDistance;

        TextMeshPro textMesh = popup.GetComponentInChildren<TextMeshPro>();
        Color startColor = textMesh.color;
        Color endColor = new Color(startColor.r, startColor.g, startColor.b, 0f);

        while (elapsedTime < popupDuration)
        {
            float t = elapsedTime / popupDuration;

            popup.transform.position = Vector3.Lerp(startPosition, endPosition, t);
            textMesh.color = Color.Lerp(startColor, endColor, t);

            elapsedTime += Time.deltaTime;
            yield return null;
        }

        Destroy(popup);
    }

    private void OnDestroy()
    {
        EffectSystem.OnDamageApplied -= OnDamageApplied;
    }
}